package  common.ui
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.text.TextField;
	
	import kernel.core.Core;
	

	public class Xdis
	{
		public function Xdis()
		{
		}
		
		
		public static function setMouseAndColorUnable(dis:InteractiveObject,isUnableMouse:Boolean = true):void{
			Core.setMouseClickAble(dis,!isUnableMouse);
		}
		/**
		 * 禁止容器和子级的鼠标事件
		 */ 
		public static function setMouseUnableContainer(dis:InteractiveObject,isUnableMouse:Boolean = true):void{
			dis.mouseEnabled = !isUnableMouse;
			if(dis is DisplayObjectContainer){
				DisplayObjectContainer(dis).mouseEnabled = !isUnableMouse;
				DisplayObjectContainer(dis).mouseChildren = !isUnableMouse;
			}
		}
		public static function followOneToAnother(dis:DisplayObject,followDis:DisplayObject):void
		{
			var stage:Stage = Core.stage;
			var sw:int = stage.stageWidth;
			var sh:int = stage.stageHeight;
			var x:int = int(sw/2-(dis.width+followDis.width)/2);
			var y:int = int(sh/2-followDis.height/2);
			if(x < 0 ) x = 0;
			if(y < 0 ) y = 0;
			followDis.x = x;
			followDis.y = y;
			dis.x = int(followDis.x+followDis.width);
			dis.y = int(followDis.y);
		}
		public static function moveY(dis:DisplayObject,my:Number,isCheck:Boolean=true):void
		{
			dis.y += my;
			if(isCheck == true)
			{
				if(dis.y < 0 ) dis.y = 0;
			}
		}
		
		/**
		 * 将显示对象的x和y坐标取整
		 */ 
		public static function intXY(dis:DisplayObject):void{
			dis.x = int(dis.x);
			dis.y = int(dis.y);
		}
		/**
		 * 将显示对象放到容器的中间
		 */ 
		public static function toCenter(dis:DisplayObject,parentWidth:Number,parentHeight:Number,disWidth:Number=0,disHeight:Number=0):void{
			if(disWidth == 0) disWidth = dis.width;
			if(disHeight == 0) disHeight = dis.height;
			dis.x = int((parentWidth-disWidth)/2);
			dis.y = int((parentHeight-disHeight)/2);
		}
		/**
		 * 返回多层子级显示对象，对象命名字符串按照从外到里顺序传递
		 * 
		 * 
		 */ 
		public static function getChild(parentContainer:DisplayObjectContainer,... args):DisplayObject{
			var dis:DisplayObject;
			dis = parentContainer;
			for(var i:int=0;i<args.length;i++){
				dis = DisplayObjectContainer(dis).getChildByName(String(args[i]));
			}
			return dis;
		}
		public static function getTextChild(parentContainer:DisplayObjectContainer,childName:String):TextField
		{
			return parentContainer.getChildByName(childName) as TextField;
		}
		public static function getSpriteChild(parentContainer:DisplayObjectContainer,childName:String):Sprite
		{
			return parentContainer.getChildByName(childName) as Sprite;
		}
		public static function getSimpleButtonChild(parentContainer:DisplayObjectContainer,childName:String):SimpleButton
		{
			return parentContainer.getChildByName(childName) as SimpleButton;
		}
		public static function getMovieClipChild(parentContainer:DisplayObjectContainer,childName:String):MovieClip
		{
			return parentContainer.getChildByName(childName) as MovieClip;
		}
		/**
		 * 清除目标 Sprite 最上面到指定fromIndex的所有子级显示对象
		 */
		public static function clearChildrens(sprite:Sprite,fromIndex:uint=0):void{
			for(var i:int=sprite.numChildren-1;i>=fromIndex;i--){
				sprite.removeChildAt(i);
			}
		}
		public static function sortChilds(sprite:Sprite,value:Number):void
		{
			for(var i:int=0;i<sprite.numChildren;i++)
			{
				sprite.getChildAt(i).y = i*value;
			}
		}
	}
}